package com.lxlx.weixinlx.service.Impl;

import com.alibaba.fastjson.JSON;
import com.lxlx.weixinlx.domain.HttpParame;
import com.lxlx.weixinlx.domain.Token;
import com.lxlx.weixinlx.domain.User;
import com.lxlx.weixinlx.service.WeiXingLoginService;
import com.lxlx.weixinlx.utils.HttpClient;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * @author liuhuan
 * @date 2020
 * @描述
 */
@Service
public class WeiXingLoginServiceImpl implements WeiXingLoginService {
    @Value("${appid}")
    private String appid;
    @Value("${secret}")
    private String secret;
    @Value("${redirect_uri}")
    private String redirect_uri;


    @Override
    public String getLogin() {
       String url = HttpParame.AUTHORIZATION_URL;
        url = url.replaceAll("APPID", appid);
        try {
            url = url.replaceAll("REDIRECT_URI", URLEncoder.encode(redirect_uri,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        url = url.replaceAll("SCOPE","snsapi_login");
        url = url.replaceAll("STATE","123");


        return url;
    }

    /**
     * 回调携带code,获取access_token
     * @param code
     * @return
     */
    @Override
    public Token getToken(String code) {

        try {
            String tokenUrl = HttpParame.ACCESS_TOKEN_URL;
            tokenUrl = tokenUrl.replaceAll("APPID", appid);
            tokenUrl = tokenUrl.replaceAll("SECRET",secret);
            tokenUrl = tokenUrl.replaceAll("CODE",code);
            System.out.println(tokenUrl);
            // 发送请求
            HttpClient client = new HttpClient(tokenUrl);
            // 发送get请求
            client.get();
            // 获取到请求的结果  json格式的字符串，把json格式的字符串转换成对象或者Map集合
            String token_content = null;
            token_content = client.getContent();
            if(token_content == null || token_content == ""){
                return null;
            }
            // 把json字符串转换成对象
            Token token = JSON.parseObject(token_content, Token.class);
            return token;


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("微信扫描登录异常...");
        }


    }

    /**
     * 获取用户信息
     * @param access_token
     * @param openid
     * @return
     */
    @Override
    public User getUser(String access_token, String openid) {
        try {
            String userUrl = HttpParame.GET_UNIONID_URL;
            userUrl = userUrl.replaceAll("ACCESS_TOKEN", access_token);
            userUrl = userUrl.replaceAll("OPENID",openid);
            System.out.println(userUrl);
            // 发送请求
            HttpClient client = new HttpClient(userUrl);
            // 发送get请求
            client.get();
            //获取用户信息
            String user_content = client.getContent();
            if(user_content == null || user_content == ""){
                return null;
            }
            // 解析json字符串
            User user = JSON.parseObject(user_content, User.class);
            return user;


        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("微信扫描登录异常...");
        }

    }
}
